//
//  ScrollViewCell.swift
//  CompareBuying
//
//  Created by 唐先才 on 16/1/28.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import Foundation
import UIKit

class ScrollViewCell: UICollectionViewCell {
    
    var sv: UIScrollView?
    var array: NSMutableArray = []
    var _model: TxcModel?
    var iconView: UIImageView?
    var iconLabel: UILabel?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func createUI() {
        sv = UIScrollView(frame: CGRect(x: 0, y: 0, width: WIDTH, height: 200))
        guard let sv = sv else {
            return
        }
        self.contentView.addSubview(sv)
        sv.pagingEnabled = true
        sv.userInteractionEnabled = true
        
    }
    
    func configCellWithArray(array: NSMutableArray) {
        guard let sv = sv else {
            return
        }
        if iconView == nil && iconLabel == nil {
            for index in 0...5 {
                iconView = UIImageView()
                iconLabel = UILabel()
                guard let iconView = iconView, iconLabel = iconLabel else {
                    return
                }
                iconView.frame = CGRect(x: CGFloat(index) * WIDTH, y: 0, width: WIDTH, height: 200)
                iconView.tag = 100 + index
                let tap = UITapGestureRecognizer(target: self, action: "tap:")
                iconView.userInteractionEnabled = true
                iconView.addGestureRecognizer(tap)
                if let model = array[index] as? TxcModel {
                    _model = model
                }
                guard let iconPic = _model?.ProImage, proName = _model?.ProName else {
                    return
                }
                iconView.sd_setImageWithURL(NSURL(string: iconPic))
                iconLabel.frame = CGRect(x: CGFloat(index) * WIDTH, y: 170, width: WIDTH, height: 20)
                iconLabel.text = proName
                iconLabel.textAlignment = NSTextAlignment.Center
                iconLabel.textColor = UIColor.whiteColor()
                iconLabel.font = UIFont.systemFontOfSize(13)
                sv.addSubview(iconView)
                sv.addSubview(iconLabel)
            }

        }
        sv.contentSize = CGSize(width: CGFloat(6) * WIDTH, height: 0)
    }
    
    func tap(tap: UITapGestureRecognizer) {
        
//        _model = array[(tap.view?.tag)! - 100] as? TxcModel
//        let str = String(format: (_model?.ProWebsite)!, locale: nil)
//        let string = NSURL(string: str)
//        var valueStr = string?.lastPathComponent
        
    }
    
    
    
}


